package demo

import (
	"fmt"
	"gitee.com/851537/gbase"
	"gitee.com/851537/gmodel"
	jsoniter "github.com/json-iterator/go"
)

// gorm标签 https://gorm.io/zh_CN/docs/models.html
type User struct {
	gmodel.BaseTree
	gmodel.BaseRedisLock
	Id int64 `json:"id" gorm:"primaryKey"`

	Pid      int64  `json:"pid"`
	Path     string `json:"path"`
	Uuid     int64  `json:"uuid" gorm:"index"` //flake id
	Name     string `json:"name" gorm:"size:255"`
	Nickname string `json:"nickname"`

	//手机号,唯一,登陆使用手机号登陆
	Mobile   string `json:"mobile" gorm:"size:11;index"`
	Password string `json:"password" gorm:"size:50"`
	Salt     string `json:"salt" gorm:"size:50"`

	RoleIds gbase.Path[int64] `json:"role_ids" gorm:"type:text"`

	//1.可用 2.不可用
	Status int8 `json:"status"`

	CreatedAt int64 `json:"created_at"`
	UpdatedAt int64 `json:"updated_at"`
}

func (this *User) TableName() string {
	return DbPrefix + "user"
	//return this.BaseDb.TableName("user")
}

func (this *User) GetId() int64 {
	return this.Id
}

func (this *User) GetPid() int64 {
	return this.Pid
}

func (this *User) GetPath() string {
	return this.Path
}

func (this *User) NewNode(id ...int64) gmodel.ITree {
	if len(id) > 0 {
		return &User{Id: id[0]}
	}
	return &User{}
}

func (this *User) SetRedisModel(_type int8) gmodel.IRedisSet {
	m := &gmodel.BaseRedisSetModel{}
	m.Name = fmt.Sprintf("User.%d", this.Id)
	m.Value, _ = jsoniter.MarshalToString(this)
	m.Expire = 3600 * 12 * 1
	m.DbWhere = []interface{}{"id", this.Id}
	m.DbOrder = ""
	this.BaseDbRedisSet.Init(m)
	return this
}

func (this *User) IsRootUser() bool {
	return this.Id == 1
}

//func (this *User) Claims() (claims *Claims) {
//	claims = &Claims{}
//	claims.Id = this.Id
//	claims.Uuid = this.Uuid
//	claims.Type = global.TypeUser
//	claims.RoleIds = this.RoleIds
//	return claims
//}

func NewUser(id ...int64) *User {
	if len(id) > 0 {
		return &User{Id: id[0]}
	} else {
		return &User{}
	}
}
